/**
 * 
 */
package com.abel.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.abel.domain.Application;
import com.abel.domain.AuthPrincipal;
import com.abel.domain.Education;
import com.abel.manager.EducationManager;
import com.abel.manager.exception.EducationException;
import com.abel.util.UserSessionLocator;
import com.abel.web.view.EducationInfoViewBean;


/**
 * @author admin
 *
 */
public class EducationController extends SimpleFormController {

	
    protected final Log logger = LogFactory.getLog(getClass());
    
	private EducationManager educationManager;
	 
	
	public ModelAndView handleDeleteRequest(
            HttpServletRequest request,
            HttpServletResponse response,
            EducationInfoViewBean viewBean) throws Exception {
		
		Object obj = viewBean;
		return null;
	}
	
	
    @Override
    protected ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors) throws Exception
    {
		@SuppressWarnings("unused")
		AuthPrincipal authPrincipal = UserSessionLocator.getUserSession().getAuthPrincipal();
		EducationInfoViewBean eduBean = (EducationInfoViewBean)command;
        ModelAndView mav = null;
		if (new Integer(eduBean.getDelId()).intValue() != 0 )
		{
	        try {
	        	
				this.educationManager.deleteEducationInformation(eduBean.getAplId(), eduBean.getDelId());
			
	        } 
	        catch (EducationException e) {

	        	logger.debug("@EducationInformationController : onSubmit : EducationException " + e);
	        	e.printStackTrace();
			}
	        catch (Exception e) {
	        	logger.debug("@EducationlInformationController : onSubmit : EducationException " + e);
	        	e.printStackTrace();
	        }
			
		}
		else 
		{
	        try {
				if (new Integer(eduBean.getEditId()) == 0 ){
					Education education = new Education();
					this.convertViewToEducation(education, eduBean);
					this.educationManager.updateEducationInformation(education);
				}
				else
				{
					for(Education ed : eduBean.getEduList()) 
					{
						if (eduBean.getEditId() == ed.getEduInfoId())
						{
							this.educationManager.updateEducationInformation(ed);
							break;
						}
					}
				}
	        } 
	        catch (EducationException e) {
	
	        	logger.debug("@EducationInformationController : onSubmit : EducationException " + e);
	        	e.printStackTrace();
	        	mav = new ModelAndView(getFormView());
				mav.addObject("educationInfoView", (EducationInfoViewBean)command);
	        	return mav;
			}
	        catch (Exception e) {
	
	        	logger.debug("@EducationlInformationController : onSubmit : EducationException " + e);
	        	e.printStackTrace();
	        	mav = new ModelAndView("error");
	        	return mav;
	        }
		}
		return new ModelAndView("redirect:" + getSuccessView());

    }


    @Override
    protected Object formBackingObject(HttpServletRequest request)
            throws Exception
    {

		Application application = UserSessionLocator.getUserSession().getPerson().getApplication();

		AuthPrincipal authPrincipal = UserSessionLocator.getUserSession().getAuthPrincipal();
		
		String editIdStr = request.getParameter("editId");
		String delIdStr = request.getParameter("delId");
		if (delIdStr != null) {
	        try {
				this.educationManager.deleteEducationInformation(Integer.parseInt(delIdStr));
	        }
	        catch (EducationException e) {

	        	logger.debug("@EducationInformationController : onSubmit : EducationException " + e);
	        	e.printStackTrace();
	        	//TODO - set error message
			}
	        catch (Exception e) {
	        	
	        	logger.debug("@EducationlInformationController : onSubmit : EducationException " + e);
	        	e.printStackTrace();
	        	//TODO - set error message
	        }
		}
		
		EducationInfoViewBean viewBean = new EducationInfoViewBean();
    	viewBean.setEduList(application.getEducation()); // this.educationManager.getEducationInformation(criteria));
		if (editIdStr != null) {
	    	viewBean.setEditId(Integer.parseInt(editIdStr));
	    }
	
		return viewBean;
    }

    /**
     * @param 
     */
    public void setEducationManager(EducationManager educationManager)
    {
        this.educationManager = educationManager;
    }
    
    
    private void convertViewToEducation(Education education, EducationInfoViewBean viewBean) {
    	
    	education.setAplId(UserSessionLocator.getUserSession().getPerson().getApplication().getApplicationId()); 
    	education.setEduType(viewBean.getEduType());
    	education.setEduSchoolName(viewBean.getEduSchoolName());
    	education.setEduSchoolCity(viewBean.getEduSchoolCity());
    	education.setEduSchoolState(viewBean.getEduSchoolState());
    	education.setEduGraduateYn(viewBean.getEduGraduateYn());
    	education.setEduGraduateYear(viewBean.getEduGraduateYear());
    	education.setEduGraduateMajor(viewBean.getEduGraduateMajor());
    	education.setEduDegree(viewBean.getEduDegree());
    }
}
